package com.vv.tools.modules.auth.service;

import com.coze.openapi.client.auth.LoadAuthConfig;
import com.coze.openapi.client.auth.OAuthToken;
import com.coze.openapi.service.auth.JWTOAuthClient;
import com.vv.tools.common.util.FileUtils;

import java.util.Objects;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CozeAuthService {

    public String getToken() {
        String configFilePath = Objects.requireNonNull(FileUtils.getClassPathFile("config/coze_oauth_config.json")).getPath();

        // 初始化 JWT OAuth 客户端
        JWTOAuthClient oauth;
        try {
            oauth = JWTOAuthClient.loadFromConfig(new LoadAuthConfig(configFilePath));
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }

        try {
            OAuthToken resp = oauth.getAccessToken();
            return resp.getAccessToken();
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return null;
    }

}
